package com.xyz.keshe;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xyz.keshe.adapter.AnnouncementManageAdapter;
import com.xyz.keshe.dao.AnnouncementDao;
import com.xyz.keshe.db.DatabaseHelper;
import com.xyz.keshe.model.Announcement;

import java.util.Date;
import java.util.List;

public class AnnouncementManageActivity extends AppCompatActivity {

    private ListView listViewAnnouncements;
    private Button btnAddAnnouncement;
    private AnnouncementManageAdapter adapter;
    private AnnouncementDao announcementDao;
    private List<Announcement> announcementList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_manage);

        // 初始化数据库
        announcementDao = new AnnouncementDao(this);

        // 初始化视图
        listViewAnnouncements = findViewById(R.id.listview_announcements);
        btnAddAnnouncement = findViewById(R.id.btn_add_announcement);

        // 加载公告列表
        loadAnnouncements();

        // 添加公告按钮点击事件
        btnAddAnnouncement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddAnnouncementDialog();
            }
        });
    }

    private void loadAnnouncements() {
        announcementList = announcementDao.getAllAnnouncements();
        adapter = new AnnouncementManageAdapter(this, announcementList, new AnnouncementManageAdapter.OnAnnouncementActionListener() {
            @Override
            public void onEdit(Announcement announcement) {
                showEditAnnouncementDialog(announcement);
            }

            @Override
            public void onDelete(Announcement announcement) {
                showDeleteConfirmDialog(announcement);
            }
        });
        listViewAnnouncements.setAdapter(adapter);
    }

    private void showAddAnnouncementDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("添加公告");

        View dialogView = getLayoutInflater().inflate(R.layout.dialog_add_announcement, null);
        EditText etTitle = dialogView.findViewById(R.id.et_announcement_title);
        EditText etContent = dialogView.findViewById(R.id.et_announcement_content);

        builder.setView(dialogView);
        builder.setPositiveButton("添加", (dialog, which) -> {
            String title = etTitle.getText().toString().trim();
            String content = etContent.getText().toString().trim();

            if (TextUtils.isEmpty(title)) {
                Toast.makeText(this, "公告标题不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(content)) {
                Toast.makeText(this, "公告内容不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            Announcement announcement = new Announcement();
            announcement.setTitle(title);
            announcement.setContent(content);
            announcement.setCreateTime(new Date());

            long result = announcementDao.insertAnnouncement(announcement);
            if (result > 0) {
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                loadAnnouncements();
            } else {
                Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showEditAnnouncementDialog(Announcement announcement) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("编辑公告");

        View dialogView = getLayoutInflater().inflate(R.layout.dialog_add_announcement, null);
        EditText etTitle = dialogView.findViewById(R.id.et_announcement_title);
        EditText etContent = dialogView.findViewById(R.id.et_announcement_content);

        // 填充现有数据
        etTitle.setText(announcement.getTitle());
        etContent.setText(announcement.getContent());

        builder.setView(dialogView);
        builder.setPositiveButton("保存", (dialog, which) -> {
            String title = etTitle.getText().toString().trim();
            String content = etContent.getText().toString().trim();

            if (TextUtils.isEmpty(title)) {
                Toast.makeText(this, "公告标题不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(content)) {
                Toast.makeText(this, "公告内容不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            announcement.setTitle(title);
            announcement.setContent(content);

            int result = announcementDao.updateAnnouncement(announcement);
            if (result > 0) {
                Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
                loadAnnouncements();
            } else {
                Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showDeleteConfirmDialog(Announcement announcement) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认删除");
        builder.setMessage("确定要删除公告 \"" + announcement.getTitle() + "\" 吗？");
        builder.setPositiveButton("删除", (dialog, which) -> {
            int result = announcementDao.deleteAnnouncement(announcement.getId());
            if (result > 0) {
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                loadAnnouncements();
            } else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadAnnouncements();
    }
} 